//
//  BKMyOrderDetailAlert.m
//  bestkeep
//
//  Created by Mr. Wu on 16/12/14.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKMyOrderDetailAlert.h"
#import "BKMyOrderKeyBoard.h"
#import "BK_OD_Tool.h"
@implementation BKMyOrderDetailAlert

-(instancetype)init
{
    return [self initWithFrame:CGRectZero];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createUI];
    }
    
    return self;
}

-(void)awakeFromNib
{
    [super awakeFromNib];
    [self createUI];
}

-(void)createUI{
    self.backgroundColor = RGB(248, 248, 248);
    self.layer.cornerRadius = 10;
    _promapt = [UILabel new];
    _promapt.text = @"提示";
    _promapt.textColor = RGB(51, 51, 51);
    _promapt.font = [UIFont systemFontOfSize:16];
    [self addSubview:_promapt];
    [_promapt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top).offset(19);
        make.centerX.mas_equalTo(self.mas_centerX);
    }];
    _information = [UILabel new];
    _information.text = @"确认取消此订单?";
    _information.textColor = RGB(102, 102, 102);
    _information.font = [UIFont systemFontOfSize:13];
    [self addSubview:_information];
    [_information mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top).offset(53);
        make.centerX.mas_equalTo(self.mas_centerX);
    }];
    _reason = [UITextField new];
    _reason.placeholder = @"请输入取消订单理由(必填)";
    _reason.backgroundColor = [UIColor whiteColor];
    _reason.font = [UIFont systemFontOfSize:13];
    _reason.layer.cornerRadius = 5;
    _reason.delegate = self;
    [self addSubview:_reason];
    [_reason mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.mas_top).offset(82);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.width.mas_equalTo(@(240));
        make.height.mas_equalTo(30.5);
    }];
    _line1 = [UIView new];
    _line1.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    [self addSubview:_line1];
    [_line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.mas_left);
        make.right.mas_equalTo(self.mas_right);
        make.top.mas_equalTo(self.mas_top).offset(137);
        make.height.mas_equalTo(@(1));
    }];
    _line2 = [UIView new];
    _line2.backgroundColor = [UIColor colorWithWhite:0.902 alpha:1.000];
    [self addSubview:_line2];
    [_line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_line1.mas_bottom);
        make.bottom.mas_equalTo(self.mas_bottom);
        make.width.mas_equalTo(@(0.5));
        make.centerX.mas_equalTo(self.mas_centerX);
    }];
    _cancleButton = [UIButton new];
    UIImage *Imge = [Common imageWithFrame:CGRectMake(0, 0, 135, 38) color: [UIColor colorWithWhite:0 alpha:0.2]];
    [_cancleButton setBackgroundImage:Imge forState:UIControlStateHighlighted];
    _cancleButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_cancleButton setTitle:@"取消" forState:UIControlStateNormal];
    _cancleButton.userInteractionEnabled = YES;
    [_cancleButton setTitleColor:RGB(3, 122, 255) forState:UIControlStateNormal];
    _cancleButton.titleLabel.font = [UIFont  fontWithName:@"STHeitiSC-Light"size:17];
    [_cancleButton addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_cancleButton];
    [_cancleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_line1.mas_bottom).offset(0);
        make.left.mas_equalTo(self.mas_left).offset(0);
        make.height.mas_equalTo(@(38));
        make.width.mas_equalTo(@(135));
    }];
    
    _sureButton = [UIButton new];
    _sureButton.userInteractionEnabled = NO;
    [_sureButton setBackgroundImage:Imge forState:UIControlStateHighlighted];
    _sureButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
    [_sureButton addTarget:self action:@selector(sure:) forControlEvents:UIControlEventTouchUpInside];
    [_sureButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    _sureButton.titleLabel.font = [UIFont  fontWithName:@"STHeitiSC-Light"size:17];
    [self addSubview:_sureButton];
    [_sureButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_line1.mas_bottom).offset(0);
        make.right.mas_equalTo(self.mas_right).offset(0);
        make.height.mas_equalTo(@(38));
        make.width.mas_equalTo(@(135));
    }];
    
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSLog(@"%@",string);
    if (string.length >0) {
        _sureButton.userInteractionEnabled = YES;
        [_sureButton setTitleColor:RGB(3, 122, 255) forState:UIControlStateNormal];
        
    }else{
        if (_reason.text.length>1) {
            
        }else{
            
            _sureButton.userInteractionEnabled = NO;
            [_sureButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
    }
    
    
    //    //不支持系统表情的输入
    //    if ([[[UITextInputMode currentInputMode ]primaryLanguage] isEqualToString:@"emoji"]) {
    //        return NO;
    //    }
    //    //禁止输入emoji表情
    //    if ([BKTool stringContainsEmoji:string]) {
    //        return NO;
    //    }
    return YES;
}


- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

-(void)textFieldDidBeginEditing:(UITextField *)textField{
    [BKMyOrderKeyBoard viewToolWithFrameChangeFromKeyboard:@[self] viewOffTops:@[@55]];
}
-(void)sure:(UIButton*)btn{
    if ([self isEmpty:_reason.text]) {
        NSString *str = @"请输入取消订单理由";
        _reason.placeholder = str;
        // BKAnimatedAlert *animatedAlert = [[BKAnimatedAlert alloc] init];
        //[animatedAlert BkAlert_Style:BKAlertStyle_Waring accordingDetailContentAutoLayout:str];
        return;
    }
    NSString *passwordStandard = @"^[(0-9)|(\u4e00-\u9fa5)|(A-Za-z0-9\u4e00-\u9fa5)|(@)|(!)|(#)|(+)|(-)|(%)|(,)|(<)|(，)|(。)|(.)|(/)|(?)|(;)|(；)|(：)|(:)|({)|([)|(])|(})|(*)|(~)|(`)|(·)|(《)|(》)|(>)|(？)|(“)|(&)|(……)|(…)|(^)|( )|(！)|(')|(”)|($)|(￥)|(（)|(）)]+$";
    NSRegularExpression *regexpasswordStandard = [NSRegularExpression regularExpressionWithPattern:passwordStandard options:0 error:nil];
    NSTextCheckingResult *isMatchpasswordStandard = [regexpasswordStandard firstMatchInString:_reason.text options:0 range:NSMakeRange(0, [_reason.text length])];
    
    if (!isMatchpasswordStandard) {
        UIAlertView *alter =[[UIAlertView alloc]initWithTitle:@"不能包含特殊字符" message:@"请重新输入" delegate:self cancelButtonTitle:@"确认" otherButtonTitles: nil];
        [alter show];
    }else{
        if ([self.delegate respondsToSelector:@selector(sureButtonClick:)]) {
            [self.delegate sureButtonClick:_reason.text];
        }
    }
}

-(void)cancel{
    if ([self.delegate respondsToSelector:@selector(cancelButtonClick)]) {
        [self.delegate cancelButtonClick];
    }
}

//判断内容是否全部为空格  yes 全部为空格  no 不是
- (BOOL)isEmpty:(NSString *) str {
    
    if (!str) {
        return true;
    } else {
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
    }
}


@end
